package com.east.service.Dictionary.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.east.bean.DictionaryGroups;
import com.east.bean.DictionaryItems;
import com.east.bean.Role;
import com.east.dao.DictionaryGroupsMapper;
import com.east.dao.DictionaryItemsMapper;
import com.east.service.Dictionary.DictionaryService;
import com.east.utils.ObjectUtil;
import com.east.utils.Pagenation;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service("dictionaryService")
public class DictionaryServiceImpl  implements DictionaryService{
	@Autowired
	DictionaryItemsMapper dictionaryItemsMapper;
	@Autowired
	DictionaryGroupsMapper dictionaryGroupsMapper;
	


	@Override
	public List<DictionaryItems> findByDictionaryItem(String Type) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public List<DictionaryItems> findByDictionaryItemList(String diId) {
		Example example = new Example(DictionaryItems.class);
		Criteria cr = example.createCriteria();
		cr.andEqualTo("dictionaryGroupsId", diId);
		
		
		return dictionaryItemsMapper.selectByExample(example);
	}
	@Override
	public Page<DictionaryItems> pagefindByDictionaryItem(Pagenation pagenation, String code, String name, String key) {
		Page<DictionaryItems> startPage = PageHelper.startPage(pagenation.getPageNum(), pagenation.getPageSize());		
		Map<String, Object> hashMap =new  HashMap<String, Object>();
		hashMap.put("code", code);
		hashMap.put("name", name);
		hashMap.put("key", key);
		dictionaryItemsMapper.findByDictionaryItems(hashMap);
		return startPage;
	}
	@Override
	public List<DictionaryItems> listfindByDictionaryItem(String code, String name, String key) {
		Map<String, Object> hashMap =new  HashMap<String, Object>();
		hashMap.put("code", code);
		hashMap.put("name", name);
		hashMap.put("key", key);
		return dictionaryItemsMapper.findByDictionaryItems(hashMap);
	}
	@Override
	public DictionaryItems findByOne(DictionaryItems dictionaryItems) {
		return dictionaryItemsMapper.selectByPrimaryKey(dictionaryItems.getDiId());
	}

	@Override
	public int UpdeteDictionaryItems(DictionaryItems dictionaryItems) {
		DictionaryItems dit = dictionaryItemsMapper.selectByPrimaryKey(dictionaryItems.getDiId());
		dit.setDiName(dictionaryItems.getDiName());
		dit.setDgDecription(dictionaryItems.getDgDecription());
		dit.setDiPlace(dictionaryItems.getDiPlace());
		return dictionaryItemsMapper.updateByPrimaryKey(dit);
	}

	@Override
	public int delDictionaryItems(DictionaryItems dictionaryItems) {
		return dictionaryItemsMapper.deleteByPrimaryKey(dictionaryItems.getDiId());
	}

	@Override
	public int saveDictionaryItems(String code, String name, DictionaryItems dictionaryItems,boolean state) {
		if(state) {
			DictionaryGroups group = dictionaryGroupsMapper.findByNameAndCode(code, name);
			DictionaryItems dit = new DictionaryItems();
			dit.setDiName(dictionaryItems.getDiName());
			dit.setDgDecription(dictionaryItems.getDgDecription());
			dit.setDiValue(dictionaryItems.getDiPlace());
			dit.setDiPlace(dictionaryItems.getDiPlace());
			dit.setDictionaryGroupsId(group.getDgId());
			dit.setDiCode(code+"_"+ObjectUtil.ToPinyin(dictionaryItems.getDiName()).toUpperCase());
			
			
			DictionaryGroups groups = new DictionaryGroups();
			groups.setDgCode(code+"_"+ObjectUtil.ToPinyin(dictionaryItems.getDiName()).toUpperCase());
			groups.setDgName(dictionaryItems.getDiName());
			groups.setDgDecription(dictionaryItems.getDgDecription());
			dictionaryGroupsMapper.insert(groups);
			
			return dictionaryItemsMapper.insert(dit);
		}else {
			DictionaryGroups group = dictionaryGroupsMapper.findByNameAndCode(code, name);
			DictionaryItems dit = new DictionaryItems();
			dit.setDiName(dictionaryItems.getDiName());
			dit.setDgDecription(dictionaryItems.getDgDecription());
			dit.setDiValue(dictionaryItems.getDiPlace());
			dit.setDiPlace(dictionaryItems.getDiPlace());
			dit.setDictionaryGroupsId(group.getDgId());
			dit.setDiCode(code+"_"+ObjectUtil.ToPinyin(dictionaryItems.getDiName()).toUpperCase());
			return dictionaryItemsMapper.insert(dit);
		}
		
	}
	@Override
	public List<DictionaryItems> findBystaffSale(Integer dementId) {
		return dictionaryItemsMapper.findBystaffSale(dementId);
	}
	@Override
	public List<DictionaryItems> findBystaff() {
		return dictionaryItemsMapper.findBystaff();
	}
	@Override
	public List<DictionaryItems> findByProject() {
		return dictionaryItemsMapper.findByproject();
	}
	@Override
	public List<DictionaryItems> findBySaleCompany() {
		return dictionaryItemsMapper.findBySaleCompany();
	}
	public static void main(String[] args) {
		int num=0;
		for (int i = 0; i <1000; i++) {
			if(i%7==0) {
				num+=i;
				continue;
			}
		}
		System.out.println("和="+num);
	}
}
